package com.klangner.go.model.impl;

import java.util.Collection;

import com.klangner.go.model.IBoard;
import com.klangner.go.model.IStonesGroup;
import com.klangner.go.model.Move;


/**
 * This class implements basic board functionality.
 * No rules are exposed on 
 * @author klangner
 *
 */
public class GoBoard implements IBoard{

	/** Board size */
	private int			boardSize;
	/** board data */
	private int[][] goban;
	/** Cached grouping data */
	private GroupingAlgorithm	groups = null;
	
	/**
	 * Constructor
	 */
	public GoBoard(int size){

		boardSize = size;
		initBoard();
	}
	
	/**
	 * Private constructor for creating object copy
	 */
	private GoBoard(int size, int[][] data){

		boardSize = size;
		goban = new int[boardSize][boardSize];
		for (int x = 0; x < boardSize; x++){
      for (int y = 0; y < boardSize; y++){
      	goban[x][y] = data[x][y];
      }
		}
	}

	
	/**
	 * @see IBoard#getGroups(int)
	 */
	@Override
	public Collection<IStonesGroup> getGroups(int color) {

		if(groups == null){
			groups = new GroupingAlgorithm(this);
		}
		
		return groups.getGroups(color);
	}

	
	/**
	 * @see IBoard#addStone(Move)
	 */
	@Override
	public void setStone(int x, int y, int color) {
		
		groups = null;
		goban[x][y] = color;
	}
	
	/**
	 * @see IBoard#getSize()
	 */
	@Override
	public int getSize() {
		return boardSize;
	}

	/**
	 * @see IBoard#getStone(int, int)
	 */
	@Override
	public int getStone(int x, int y) {
		return goban[x][y];
	}

	/**
	 * @see IBoard#makeCopy()
	 */
	@Override
	public IBoard makeCopy() {

		return new GoBoard(boardSize, goban);
	}

	


	/**
	 * Init board data
	 */
	private void initBoard() {
		
		goban = new int[boardSize][boardSize];
    
		for (int x = 0; x < boardSize; x++){
      for (int y = 0; y < boardSize; y++){
      	goban[x][y] = NO_STONE;
      }
		}
	}


}
